//index.js
//获取应用实例
const app = getApp()
var WxParse = require('../../wxParse/wxParse.js');
var that;
Page({
  data: {
    imgUrls: [],
    articleList: [],
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    circular: true,
    total: 1,
    article: "",
    item:[],


    userInfo: {},
    // hasUserInfo: false,
    // canIUse: wx.canIUse('button.open-type.getUserInfo')
  },

  onLoad: function () {
    that = this;
    wx.showToast({
      title: '加载中',
      icon: 'loading'
    }),
    setTimeout(() => {
      //that.dataLoadingIndex();
      that.getRecommendContentList();
    }, 1500)
   
  },
  dataLoadingIndex: function () {
    wx.request({
      url: getApp().globalData.urls + '/wxgz/article/getArticleList',
      data: {
        page: that.data.total,
        rows: '10'
      },
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded' // post:application/x-www-form-urlencoded,get:application/json
      },
      success: function (res) {
        console.log(res.data)
        var code = res.data.code
        if (code == 200) {
          var list = that.data.articleList;
          for (var i = 0; i < res.data.data.length; i++) {
            list.push(res.data.data[i]);
          }
          //WxParse.wxParse('article', 'html', list[0].content, that, 5)
          that.setData({
            articleList: list
          })
        } else {

        }

      }
    })
  },

  // 下拉刷新回调接口
  onPullDownRefresh: function () {
    // 我们用total和count来控制分页，total代表已请求数据的总数，count代表每次请求的个数。
    // 刷新时需把total重置为0，代表重新从第一条请求。
    that.data.total = 1;
    // this.data.dataArray 是页面中绑定的数据源
    that.data.articleList = [];

   
    // 网络请求，重新请求一遍数据
    that.getRecommendContentList();
    // 小程序提供的api，通知页面停止下拉刷新效果

    wx.stopPullDownRefresh();
  },
  onReachBottom: function () {
    wx.showToast({
      title: '加载中',
      icon: 'loading'
    })
    that.data.total = that.data.total + 1;
    setTimeout(() => {
      that.getRecommendContentList();
      wx.stopPullDownRefresh();
    }, 1500)
    
  },
  onTap: function (e) {
    var data = e.currentTarget.dataset.data
    console.log(data)
    if (data.type == "PETSHOW") {
      var transData = JSON.stringify(data); 
      wx.navigateTo({
        url: '../publishContent/publishContent?id=' + data.data.id + "&sends=" + data.data.sends + "&zans=" + data.data.zans + "&zanState=" + data.data.zanState,
      })
    }else{
      var id = data.data.id
      wx.navigateTo({
        url: '../articleDetail/articleDetail?id=' + id
      })
    }
    
  },


  choosePet: function () {
    wx.navigateTo({
      url: '../choosePet/choosePet'  //跳转页面的路径，可带参数 ？隔开，不同参数用 & 分隔；相对    路径，不需要.wxml后缀
    })
  },
  raisePet: function () {
    wx.navigateTo({
      url: '../keepAPet/keepAPet'  //跳转页面的路径，可带参数 ？隔开，不同参数用 & 分隔；相对    路径，不需要.wxml后缀
    })
  },

  onShareAppMessage:function(e){
    
    if (e.from === 'button') {
      
      console.log(e.target)

      var data = e.target.dataset.item
      var id = data.id
      // 来自页面内转发按钮
      return {
        title: data.title,
        path: '/pages/articleDetail/articleDetail?id=' + data.id,
        imageUrl: data.upimageUrl,
        success: function (res) {

        },
        fail: function (res) {

        }
      }

    }

    
  },

/**
 * 说说图片预览
 */
  Image: function (e) {
    var data = e.currentTarget.dataset.data
    var item = e.currentTarget.dataset.item
    //console.log(data.data.imageUrl)
    // 预览图集
    wx.previewImage({
      current: item,
      urls: data.data.imageUrl
    });
  },
  
  /**
   * 
   */
  getRecommendContentList:function(){
    wx.request({
      url: getApp().globalData.urls + '/wxgz/content/getRecommendContentList',
      data: {
        times: that.data.total,
        token:wx.getStorageSync("token")
      },
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded' // post:application/x-www-form-urlencoded,get:application/json
      },
      success: function (res) {
        console.log(res.data)
        var code = res.data.code
        if (code == 200) {
          var list = that.data.articleList;
          for (var i = 0; i < res.data.data.length; i++) {
            var item = res.data.data[i];
            if (item.type == "PETSHOW"){
              item.data.imageUrl = item.data.imageUrl.split(',');
            }
            list.push(item);
            
          }
          //WxParse.wxParse('article', 'html', list[0].content, that, 5)
          that.setData({
            articleList: list
          })
        } else {

        }
      }
    })
  },

  /**
 *  关联宠物
 */
  details: function (e) {
    var datas = e.currentTarget.dataset.data
    console.log(datas)
   
    wx.request({
      url: getApp().globalData.urls + '/wxgz/pet/getPetInfo',
      method: "GET",
      data: {
        petId: datas.data.petId
      },
      header: {
        'content-type': 'application/json' // post:application/x-www-form-urlencoded,get:application/json
      },
      success: function (res) {
        console.log(res.data)
        if (res.data.code == 200) {
          wx.navigateTo({
            url: '../petCardInformation/petCardInformation?item=' + JSON.stringify(res.data.data),
          })
        }
      }
    })
  },
})
